﻿using System.Collections.Generic;
using System.Linq;

namespace SelfDividingNumbers
{
    public class Solution
    {
        public IList<int> SelfDividingNumbers(int left, int right)
        {
            var numbers = new List<int>();
            for (int i = left; i <= right; i++)
            {
                var digits = this.DivideNumber(i);
                if (IsSelfDividing(i, digits))
                {
                    numbers.Add(i);
                }
            }
            return numbers;
        }

        private IList<int> DivideNumber(int n)
        {
            var digits = new List<int>();
            while (n > 0)
            {
                digits.Add(n % 10);
                n /= 10;
            }

            return digits;
        }

        private bool IsSelfDividing(int n, IList<int> digits)
        {
            return digits.All(d => d > 0 && n % d == 0);
        }
    }
}
